<?php
if(!Authenticate::isLogin()){
	throw new Exception('You not login');
}
$pdo = Db::getPDO();

$st = $pdo->prepare("SELECT * FROM Product WHERE dateTimeout>=NOW() AND accountId=:accountId ORDER BY createTime");
$st->execute(array('accountId'=>Authenticate::getId()));
$products = $st->fetchAll(PDO::FETCH_ASSOC);
$st2 = $pdo->prepare("SELECT * FROM Invoice WHERE productId=:productId ORDER BY createTime DESC LIMIT 1");
$st3 = $pdo->prepare("SELECT * FROM Package WHERE id=:id LIMIT 1");
foreach($products as $key  => $product){
    $st2->execute(array('productId'=> $product['id']));
    $st3->execute(array('id'=> $product['packageId']));
    $products[$key]['invoice'] = $st2->fetch(PDO::FETCH_ASSOC);
    $products[$key]['package'] = $st3->fetch(PDO::FETCH_ASSOC);
}
$st = $pdo->prepare("SELECT * FROM Invoice WHERE isNew=1 AND accountId=:accountId AND dateDue>=NOW() AND productId IS NULL ORDER BY createTime");
$st->execute(array('accountId'=> Authenticate::getId()));
$newInvoices = $st->fetchAll(PDO::FETCH_ASSOC);
foreach($newInvoices as $key => $newInvoice){
    $st3->execute(array('id'=> $newInvoice['packageId']));
    $arr['package'] = $st3->fetch(PDO::FETCH_ASSOC);
    $arr['invoice'] = $newInvoice;
    array_unshift($products, $arr);
}

View::display('user/menu', array());
View::display('user/product', array('products'=> $products));